<?php

/**
 * home actions.
 *
 * @package    dnevnik
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class userActions extends sfActions
{
  /**
  *@desc klasicna autentifikacija, posle uspesnog logina
  * ista funckcija i u modulu profil
  */
  private function authenticate($profil)
  {
      $profil->setLastActiv(time());
      $profil->save();
      $this->getUser()->setAuthenticated(true);
      $this->getUser()->setAttribute("id", $profil->getId());
      $this->getUser()->setAttribute("tip", $profil->getTip());

      if($profil->getTip() === ProfilPeer::$tip['roditelj'])
      {
        $this->getUser()->addCredential("roditelj");
        $roditelj = DbFinder::from('Roditelj')->findPk($profil->getId());
        if(!($roditelj->getClanarina()))
        {
          $roditelj->setClanarina(date('Y-n-d', time() + 7 * 24 * 60 * 60));
          $roditelj->save();
        }
      }

      else if($profil->getTip() === ProfilPeer::$tip['ucenik'])
      {
        $this->getUser()->addCredential("ucenik");
      }
  }

  /**
  *@desc klasicna autentifikacija, provera username-a i passworda
  */
  public function executeLogin($id)
  {
     $disabled_skolas = array();
      if ($this->getRequest()->getMethod() != sfRequest::POST)
      {
          // display the form
          $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
      }
      else
      {
          // handle the form submission
          $username = $this->getRequestParameter('username');
          $password = $this->getRequestParameter('password');

          $profil = DbFinder::from('Profil')->where('Username', $username)->where('Password', md5($password))->findOne();
          if($profil)
          {
             $tips = ProfilPeer::getTips();
             $tip = ucwords($tips[$profil->getTip()]);
             if($tip == 'Profesor' or $tip == 'Roditelj' or $tip == 'Ucenik')
             {
               $skola_id_1 = DbFinder::from('Profil')->join($tip)->select('SkolaId')->withColumn($tip.'.SkolaId', 'SkolaId')->findPk($profil->getId());
               $skola_ids = array($skola_id_1);
             }

             else if($tip == 'Upravnik')
             {
                $skola_ids = DbFinder::from('Profil')->join('Upravnik')->join('UpravnikSkola')->select('SkolaId')->withColumn('UpravnikSkola.SkolaId', 'SkolaId')->where('Profil.Id', $profil->getId())->find();
             }

             else if($tip == 'Operater')
             {
                $skola_ids = DbFinder::from('Profil')->join('Operater')->join('OperaterRazred')->join('Razred')->select('SkolaId')->withColumn('Razred.SkolaId', 'SkolaId')->where('Profil.Id', $profil->getId())->groupBy('Razred.SkolaId')->find();
             }
             foreach($skola_ids as $skola_id)
               if(in_array($skola_id, $disabled_skolas))
                 die('Greska pri logiranju!!!');


              $logiranje = new Logiranje();
              $logiranje->setProfilId($profil->getId());
              $logiranje->setTip(LogiranjePeer::$tip['login']);
              $logiranje->save();

              if($profil->getTip() === ProfilPeer::$tip['roditelj'] or $profil->getTip() === ProfilPeer::$tip['ucenik'])
              {
                  $this->authenticate($profil);
                  $this->redirect('home/welcome');
              }

              else if($profil->getTip() === ProfilPeer::$tip['administrator'])
              {
                  $data = array('username' => $username, 'password' => $password);
                  $uri = "/backend.php/user/login";
                  die(Functions::postRedirect($data, $uri));
              }

              else if($profil->getTip() === ProfilPeer::$tip['operater'])
              {
                  $data = array('username' => $username, 'password' => $password);
                  $uri = "/administracija.php/user/login";
                  die(Functions::postRedirect($data, $uri));
              }

              else if($profil->getTip() === ProfilPeer::$tip['profesor'])
              {
                  $data = array('username' => $username, 'password' => $password);
                  $uri = '/skola.php/user/login';
                  die(Functions::postRedirect($data, $uri));
              }

              else if($profil->getTip() === ProfilPeer::$tip['upravnik'])
              {
                  $data = array('username' => $username, 'password' => $password);
                  $uri = "/uprava.php/user/login";
                  die(Functions::postRedirect($data, $uri));
              }
          }
          $this->redirect('home/pocetna');
          return sfView::NONE;
      }
  }

  /**
  *@desc logout i redirect na homepage
  */
  public function executeLogout(sfWebRequest $request)
  {
      if($this->getUser()->getAttribute('id'))
      {
        $logiranje = new Logiranje();
        $logiranje->setProfilId($this->getUser()->getAttribute('id'));
        $logiranje->setTip(LogiranjePeer::$tip['logout']);
        $logiranje->save();
      }

      $this->getUser()->setAuthenticated(false);
      $this->getUser()->clearCredentials();
      $this->getUser()->getAttributeHolder()->clear();
      $this->redirect('@homepage');
      return sfView::NONE;
  }
}